package com.atguigu.juc.study.chapter08.cas;

import java.util.concurrent.atomic.AtomicReference;

/**
 * @author : leimingchao
 * @date Date : 2022年06月14日 22:32
 */
public class SpinLock {

    AtomicReference<Thread> atomicReference = new AtomicReference<>();

    public void lock() {

        Thread thread = Thread.currentThread();
        System.out.println(thread.getName() + "\t" + "----come in");

        while (!atomicReference.compareAndSet(null, thread)) {

        }
    }

    public void unlock() {
        Thread thread = Thread.currentThread();
        atomicReference.compareAndSet(thread, null);
        System.out.println(thread.getName() + "\t" + "task over,unlock...");
    }
}
